package com.cares.event.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cares.event.dao.EventProcessDao;
import com.cares.event.dto.EventProcessDto;
import com.cares.event.entity.TEventLob;
import com.cares.event.entity.TEventProcess;
import com.cares.event.service.EventProcessService;

@Service("eventProcessService")
public class EventProcessServiceImpl implements EventProcessService {
	
	@Autowired
	private EventProcessDao eventProcessDao;
	

	/**
	 * 查询流程
	 * @param jsflowProcessVO
	 */
	public List<TEventProcess> queryEventflowProcess(String jsflowClass, String state){
		return eventProcessDao.queryEventProcess(jsflowClass, state);
	}

	/**
	 * 查询流程
	 * @param 主键
	 */
	public TEventProcess getEventProcess(Long id){
		return eventProcessDao.getEventProcess(id);
	}
	
	/**
	 * 保存流程
	 * @param jsflowProcessVO
	 */
	public void doSaveEventProcess(EventProcessDto jsflowProcessDto){
		
		TEventProcess jsflowProcessVO = new TEventProcess();
		BeanUtils.copyProperties(jsflowProcessDto, jsflowProcessVO);
		TEventLob jsflowLobVO = new TEventLob();
		BeanUtils.copyProperties(jsflowProcessDto.getEventName(), jsflowLobVO);
		jsflowProcessVO.setTEventLob(jsflowLobVO);
		eventProcessDao.saveEventProcess(jsflowProcessVO);
	}
	
	/**
	 * 更新流程
	 * @param jsflowProcessVO
	 */
	public void doUpdateEventProcess(EventProcessDto jsflowProcessDto){
		TEventProcess jsflowProcess= eventProcessDao.getEventProcess(jsflowProcessDto.getEventFlowId());
		if(null != jsflowProcessDto.getEventClass()){
			jsflowProcess.setEventClass(jsflowProcessDto.getEventClass());
		}
		if(null != jsflowProcessDto.getEventFlowId()){
			jsflowProcess.setEventFlowId(jsflowProcessDto.getEventFlowId());
		}
		if(StringUtils.isNotBlank(jsflowProcessDto.getEventName())){
			jsflowProcess.setEventName(jsflowProcessDto.getEventName());
		}
		if(null != jsflowProcessDto.getEventTime()){
			jsflowProcess.setEventTime(jsflowProcessDto.getEventTime());
		}
		if(null != jsflowProcessDto.getEventVarsion()){
			jsflowProcess.setEventVarsion(jsflowProcessDto.getEventVarsion());
		}
		if(null != jsflowProcessDto.getLobId()){
			jsflowProcess.setLobId(jsflowProcessDto.getLobId());
		}
		if(StringUtils.isNotBlank(jsflowProcessDto.getLoopRun())){
			jsflowProcess.setLoopRun(jsflowProcessDto.getLoopRun());
		}
		if(null != jsflowProcessDto.getLoopSpaceTime()){
			jsflowProcess.setLoopSpaceTime(jsflowProcessDto.getLoopSpaceTime());
		}
		if(StringUtils.isNotBlank(jsflowProcessDto.getStartNow())){
			jsflowProcess.setStartNow(jsflowProcessDto.getStartNow());
		}
		if(null != jsflowProcessDto.getStartTime()){
			jsflowProcess.setStartTime(jsflowProcessDto.getStartTime());
		}
		if(StringUtils.isNotBlank(jsflowProcessDto.getState())){
			jsflowProcess.setState(jsflowProcessDto.getState());
		}
		if(null != jsflowProcess.getTEventLob()){
			TEventLob jsflowLobVO = jsflowProcess.getTEventLob();
			if(StringUtils.isNotBlank(jsflowProcessDto.getEventLobDto().getEventXml())){
				jsflowLobVO.setEventXml(jsflowProcessDto.getEventLobDto().getEventXml());
			}
			if(null != jsflowProcessDto.getEventLobDto().getLobId()){
				jsflowLobVO.setLobId(jsflowProcessDto.getEventLobDto().getLobId());
			}
		}
		eventProcessDao.updateEventProcess(jsflowProcess);
	}
	
	
}
